package com.google.code.netbuilder.dal.providersimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.google.code.netbuilder.dal.entities.CableWire;
import com.google.code.netbuilder.dal.entities.EquipmentVersion;
import com.google.code.netbuilder.dal.providers.IEquipmentVersion;
import com.google.code.netbuilder.dal.services.JDBCServise;

public class EquipmentVersionJDBC implements IEquipmentVersion {

	@Override
	public void save(EquipmentVersion entity) {
		String query = "INSERT INTO equpment_version(equipment_id, field_name, value, version, id)" +
				" VALUES (?,?,?,?,?);";
		Connection con = null;
		PreparedStatement pst = null;
		try
		{
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			int rs=-1;
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getEquipmentId());
			pst.setString(2, entity.getFieldName());
			pst.setString(3, entity.getValue());
			pst.setInt(4, entity.getVersion());
			pst.setInt(5, entity.getId());
			pst.executeUpdate();
		}
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}

	}

	@Override
	public void update(EquipmentVersion entity) {
		String query = "UPDATE equpment_version SET equipment_id=?, field_name=?, value=?," +
				" version=?  WHERE id=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getEquipmentId());
			pst.setString(2, entity.getFieldName());
			pst.setString(3, entity.getValue());
			pst.setInt(4, entity.getVersion());
			pst.setInt(5, entity.getId());
			pst.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}

	}

	@Override
	public void delete(EquipmentVersion entity) {
		String query = "DELETE FROM equpment_version WHERE id=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getId());	
			pst.executeUpdate();	
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}		
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}

	}

	@Override
	public List findAll() {
		
		return getListEquipmentVersion("Select * From equpment_version;");
	}

	@Override
	public EquipmentVersion findByID(Integer id) {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		EquipmentVersion e = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement("Select * From equpment_version where id="+id.toString());
			rs = pst.executeQuery();
        
			if(!rs.first()) throw new SQLException("EquipmentVersion not found");;
						
			e = new EquipmentVersion();
			e.setId(rs.getInt("id"));
			e.setEquipmentId(rs.getInt("equipment_id"));
			e.setFieldName(rs.getString("field_name"));
			e.setValue(rs.getString("value"));
			e.setVersion(rs.getInt("version"));			
			
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}
		
        return e;
	}

	@Override
	public List<EquipmentVersion> getEquipmentVersionsByVersion(Integer version) {
		
		return getListEquipmentVersion("Select * From equpment_version where version="
										+version.toString()+";");
	}
	
	private List<EquipmentVersion> getListEquipmentVersion(String query)
	{
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<EquipmentVersion> ev = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement(query);
			rs = pst.executeQuery();
        
			ev = new ArrayList<EquipmentVersion>();
        
			while (rs.next()) {
				EquipmentVersion e = new EquipmentVersion();
				e.setId(rs.getInt("id"));
				e.setEquipmentId(rs.getInt("equipment_id"));
				e.setFieldName(rs.getString("field_name"));
				e.setValue(rs.getString("value"));
				e.setVersion(rs.getInt("version"));
				ev.add(e);
			}
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}		
        return ev;
	}

}
